<?php

namespace app\admin\controller;

use app\common\Common;
use app\common\Error;
use app\common\JsonRpcClient;
use app\Request;

class OpenWeixin extends Base
{
	private static $jsonRpcClient;

	public function __construct()
	{
		JsonRpcClient::config(config('jsonrpc.address'));
		JsonRpcClient::$timeout = 10;
		self::$jsonRpcClient = JsonRpcClient::instance('admin/v1/OpenWeixin');
	}

	/**
	 * 获取用户授权页 URL
	 * 
	 * @author dotdotdot
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   url[data] 授权地址
	 */
	public function getPreAuthorizationUrl(Request $request)
	{
		$rpcData = self::$jsonRpcClient->getPreAuthorizationUrl();

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取授权方的帐号基本信息
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   [data]   https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/authorization-management/getAuthorizerInfo.html
	 */
	public function getAuthorizer(request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getAuthorizer($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取已授权的授权方列表
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   authorizer_appid[data] 已授权帐号的 appid
	 * @return string|null   refresh_token[data] 刷新令牌authorizer_refresh_token
	 * @return int   auth_time[data] 授权的时间
	 */
	public function getAuthorizers(request $request)
	{
		$params = [
			'page' => $request->get('page'),
			'limit' => $request->get('limit')
		];

		$rpcData = self::$jsonRpcClient->getAuthorizers($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 查询小程序版本信息
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   [data]   https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/code/get_versioninfo.html
	 */
	public function getVersionInfo(request $request)
	{
		$params = [
			'app_id' => $request->get('app_id'),
		];

		$rpcData = self::$jsonRpcClient->getVersionInfo($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取体验版二维码
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   image_name[data]   图片名
	 * @return string   image_ext[data]   图片后缀
	 * @return string   image[data]   图片,base_encode64编码
	 *
	 */
	public function getQrCode(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getQrCode($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取草稿箱列表
	 * 
	 * @author dotdotdot
	 * 
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   [data]   https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/thirdparty-management/template-management/getTemplatedRaftList.html
	 */
	public function getTemplatedRaft(Request $request)
	{
		$rpcData = self::$jsonRpcClient->getTemplatedRaft();

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 将草稿添加到模板库
	 * 
	 * @author dotdotdot
	 * 
	 * @param int $draft_id 草稿id
	 * @param int $template_type 0:普通模板,1:标准模板库
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 */
	public function postTotemplate(Request $request)
	{
		$params = [
			'draft_id' => $request->post('draft_id'),
			'template_type' => $request->post('template_type')
		];

		$rpcData = self::$jsonRpcClient->postTotemplate($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 获取模板列表
	 * 
	 * @author dotdotdot
	 * 
	 * @param int $template_type 0:普通模板,1:标准模板库
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   [data]   https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/thirdparty-management/template-management/getTemplateList.html
	 */
	public function getTemplate(Request $request)
	{
		$params = [
			'template_type' => $request->get('template_type')
		];

		$rpcData = self::$jsonRpcClient->getTemplate($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 删除代码模板
	 * 
	 * @author dotdotdot
	 * 
	 * @param int $template_id 模板id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 */
	public function deleteTemplate(Request $request)
	{
		$params = [
			'template_id' => $request->post('template_id')
		];

		$rpcData = self::$jsonRpcClient->deleteTemplate($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 上传代码并生成体验版
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param int $template_id 代码库中的代码模板id
	 * @param array $ext_json https://developers.weixin.qq.com/miniprogram/dev/devtools/ext.html
	 * @param string $user_version 代码版本号，开发者可自定义（长度不要超过 64 个字符）
	 * @param string $user_desc 代码描述，开发者可自定义
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 */
	public function postCommit(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'template_id' => $request->post('template_id'),
			'ext_json' => $request->post('ext_json'),
			'user_version' => $request->post('user_version'),
			'user_desc' => $request->post('user_desc')
		];

		$rpcData = self::$jsonRpcClient->postCommit($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 获取类目名称信息
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string first_class[data] 一级类目
	 * @return string second_class[data] 二级类目
	 * @return int third_class[data] 三级类目
	 * @return int first_id[data]  一级类目id
	 * @return int second_id[data] 二级类目id
	 * @return int third_id[data]  三级类目id
	 * 
	 */
	public function getAllCategoryName(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getAllCategoryName($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 提交代码审核
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param array  https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/code-management/submitAudit.html
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return int auditid[data] 审核编号
	 * 
	 */
	public function postSubmitAudit(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'item_list' => $request->post('item_list')
		];

		$rpcData = self::$jsonRpcClient->postSubmitAudit($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 查询审核单状态
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param int $auditid  审核 id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return int status[data] 审核状态
	 * @return string reason[data] 当 status = 1 时，返回的拒绝原因; status = 4 时，返回的延后原因
	 * @return string screenshot[data] 当 status = 1 时，会返回审核失败的小程序截图示例。用竖线分隔的 media_id 的列表，可通过获取永久素材接口拉取截图内容
	 * 
	 */
	public function getAuditStatus(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id'),
			'auditid' => $request->get('auditid')
		];

		$rpcData = self::$jsonRpcClient->getAuditStatus($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 撤回代码审核
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function postUndoAudit(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id')
		];

		$rpcData = self::$jsonRpcClient->postUndoAudit($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 发布已通过审核的小程序
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function postRelease(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id')
		];

		$rpcData = self::$jsonRpcClient->postRelease($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 小程序版本回退
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function postRevertCodeRelease(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id')
		];

		$rpcData = self::$jsonRpcClient->postRevertCodeRelease($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 查询最新一次审核单状态
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return int   auditid[data]      最新的审核id
	 * @return int   status[data]      审核状态
	 * @return string   reason[data]      当审核被拒绝时，返回的拒绝原因
	 * @return string   screenshot[data]      当审核被拒绝时，会返回审核失败的小程序截图示例。用 竖线I 分隔的 media_id 的列表，可通过获取永久素材接口拉取截图内容
	 * @return string   user_version[data]      审核版本
	 * @return string   user_desc[data]      版本描述
	 * @return int   submit_audit_time[data]      时间戳，提交审核的时间
	 * 
	 */
	public function getLatestAuditStatus(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getLatestAuditStatus($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 加急代码审核
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param int $auditid  审核 id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function postSpeedupCodeAudit(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'auditid' => $request->post('auditid')
		];

		$rpcData = self::$jsonRpcClient->postSpeedupCodeAudit($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 新增临时素材
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $media_type  媒体文件类型，分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb）
	 * @param string $file  文件
	 * @param string $file_name  上传文件名
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   type[data]      媒体文件类型，分别有图片（image）、语音（voice）、视频（video）和缩略图（thumb，主要用于视频与音乐格式的缩略图）
	 * @return string   thumb_media_id[data]      临时素材id
	 * @return int      created_at[data]      创建时间
	 * @return array   item[data]      
	 * 
	 * 
	 */
	public function postMediaUpload(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'media_type' => $request->post('media_type'),
			'file' => base64_encode(file_get_contents($request->file('file')->getRealPath())),
			'file_name' => $request->file('file')->getOriginalName()
		];

		$rpcData = self::$jsonRpcClient->postMediaUpload($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 配置小程序服务器域名
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $action 操作类型:add,delete,set,get
	 * @param array $requestdomain    request 合法域名；当 action 是 get 时不需要此字段
	 * @param array $wsrequestdomain  socket 合法域名；当 action 是 get 时不需要此字段
	 * @param array $uploaddomain     uploadFile 合法域名；当 action 是 get 时不需要此字段
	 * @param array $downloaddomain   downloadFile 合法域名；当 action 是 get 时不需要此字段
	 * @param array $udpdomain        udp 合法域名；当 action 是 get 时不需要此字段
	 * @param array $tcpdomain        tcp 合法域名；当 action 是 get 时不需要此字段
	 *  
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 * 
	 */
	public function modifyServerDomain(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'action' => $request->post('action'),
			'requestdomain' => $request->post('requestdomain'),
			'wsrequestdomain' => $request->post('wsrequestdomain'),
			'uploaddomain' => $request->post('uploaddomain'),
			'downloaddomain' => $request->post('downloaddomain'),
			'udpdomain' => $request->post('udpdomain'),
			'tcpdomain' => $request->post('tcpdomain')
		];

		$rpcData = self::$jsonRpcClient->modifyServerDomain($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 获取业务域名校验文件
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 *  
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string      file_name[data]      文件名
	 * @return string   file_content[data]      文件内容
	 * 
	 */
	public function getJumpDomainConfirmFile(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getJumpDomainConfirmFile($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取发布后生效服务器域名列表
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 *  
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return array    [data]      https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/domain-management/getEffectiveServerDomain.html
	 * 
	 */
	public function getEffectiveServerDomain(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getEffectiveServerDomain($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取可设置的所有类目
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 *  
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return array    [data]      https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/category-management/getAllCategories.html
	 * 
	 */
	public function getAllCategories(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getAllCategories($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取已设置的所有类目
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 *  
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return array    [data]      https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/category-management/getSettingCategories.html
	 * 
	 */
	public function getSettingCategories(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getSettingCategories($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 添加类目
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param array $categories https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/category-management/addCategory.html
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function postCategory(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'categories' => $request->post('categories')
		];

		$rpcData = self::$jsonRpcClient->postCategory($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 删除类目
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param int $first  一级类目 ID
	 * @param int $second 二级类目 ID
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function deleteCategory(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'first' => $request->post('categories'),
			'second' => $request->post('second')
		];

		$rpcData = self::$jsonRpcClient->deleteCategory($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 修改类目资质信息
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param array $categories https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/category-management/modifyCategory.html
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function postModifyCategory(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'categories' => $request->post('categories')
		];

		$rpcData = self::$jsonRpcClient->postModifyCategory($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 获取系统已授权的信息
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id     小程序id
	 * @param string $app_name  小程序名称
	 * @param int $company_entity_id  公司主体id
	 * @param int $page                页数
	 * @param int $limit               数量
	 *
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   app_id[data][data]      小程序id
	 * @return string   app_name[data][data]      小程序名称
	 * @return int      company_entity_id[data][data]      公司主体id
	 * @return string   company_name[data][data]      公司主体名称
	 * @return string   audit_status[data][data]      审核状态
	 * @return string   audit_auditid[data][data]      审核单id
	 * @return int   count[data]      当前数量
	 * @return int   total_count[data]      总数
	 *
	 * 
	 */
	public function getAuthorizersDetails(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id'),
			'app_name' => $request->get('app_name'),
			'company_entity_id' => $request->get('company_entity_id'),
			'page' => $request->get('page'),
			'limit' => $request->get('limit'),
		];

		$rpcData = self::$jsonRpcClient->getAuthorizersDetails($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 设置小程序用户隐私保护指引
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id     小程序id
	 * @param array https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/privacy-management/setPrivacySetting.html#%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0      
	 *
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 *
	 * 
	 */
	public function postSetPrivacySetting(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'privacy_ver' => $request->post('privacy_ver'),
			'setting_list' => $request->post('setting_list'),
			'sdk_privacy_info_list' => $request->post('sdk_privacy_info_list'),
			'owner_setting' => $request->post('owner_setting')
		];

		$rpcData = self::$jsonRpcClient->postSetPrivacySetting($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 设置小程序用户隐私保护指引
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id     小程序id
	 * @param array https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/privacy-management/setPrivacySetting.html#%E8%AF%B7%E6%B1%82%E5%8F%82%E6%95%B0      
	 *
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return array    https://developers.weixin.qq.com/doc/oplatform/openApi/OpenApiDoc/miniprogram-management/privacy-management/getPrivacySetting.html
	 *
	 * 
	 */
	public function getPrivacySetting(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getPrivacySetting($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 上传小程序用户隐私保护指引
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $file  文件
	 * @param string $file_name  上传文件名
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   ext_file_media_id[data]      文件的media_id
	 * 
	 * 
	 */
	public function postUploadPrivacySetting(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'file' => base64_encode(file_get_contents($request->file('file')->getRealPath())),
			'file_name' => $request->file('file')->getOriginalName()
		];

		$rpcData = self::$jsonRpcClient->postSetPrivacySetting($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 查询各版本用户占比
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   now_version[data]      当前版本
	 * @return array   version[data][data]      基础库版本号
	 * @return array   percentage[data][data]      该版本用户占比
	 * 
	 * 
	 */
	public function getSupportVersion(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getSupportVersion($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 设置最低基础库版本
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $version 为已发布的基础库版本号
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 * 
	 */
	public function postSetSupportVersion(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'version' => $request->post('version')
		];

		$rpcData = self::$jsonRpcClient->postSetSupportVersion($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 获取个人模板列表
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   priTmplId[data]      添加至账号下的模板 id，发送小程序订阅消息时所需
	 * @return string   title[data]      模版标题
	 * @return string   content[data]      模版内容
	 * @return string   example[data]      模板内容示例
	 * @return int   type[data]      模版类型，2 为一次性订阅，3 为长期订阅
	 * @return string   keywordCode[keywordEnumValueList][data]      枚举参数的 key
	 * @return array   enumValueList[keywordEnumValueList][data]      枚举参数值范围列表
	 * 
	 * 
	 */
	public function getMessageTemplateList(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getMessageTemplateList($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 添加模板
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $tid           模板标题 id，可通过接口获取，也可登录小程序后台查看获取
	 * @param [int]array $kidList 开发者自行组合好的模板关键词列表，关键词顺序可以自由搭配（例如 [3,5,4] 或 [4,5,3]），最多支持5个，最少2个关键词组合
	 * @param string $sceneDesc   服务场景描述，15个字以内
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   priTmplId[data]      添加至账号下的模板id，发送小程序订阅消息时所需
	 * 
	 * 
	 */
	public function postMessageTemplate(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'tid' => $request->post('tid'),
			'kidList' => $request->post('kidList'),
			'sceneDesc' => $request->post('sceneDesc')
		];

		$rpcData = self::$jsonRpcClient->postMessageTemplate($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 删除模板
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $priTmplId           要删除的模板id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 * 
	 */
	public function deleteMessageTemplate(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'priTmplId' => $request->post('priTmplId')
		];

		$rpcData = self::$jsonRpcClient->deleteMessageTemplate($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 获取关键词列表
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $tid           模板标题 id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return int   kid[data]      关键词 id，选用模板时需要
	 * @return int   name[data]      	关键词内容
	 * @return string   example[data]     关键词内容对应的示例
	 * @return string   rule[data]      参数类型
	 * 
	 * 
	 */
	public function getPubTemplateKeyWordsById(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'tid' => $request->post('tid')
		];

		$rpcData = self::$jsonRpcClient->getPubTemplateKeyWordsById($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取所属类目下的公共模板
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param array $ids           类目 id
	 * @param int $page                页数
	 * @param int $limit               数量
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return int   tid[data]      模版标题 id
	 * @return string   title[data]      	模版标题
	 * @return int   type[data]     模版类型，2 为一次性订阅，3 为长期订阅
	 * @return int   categoryId[data]      模版所属类目 id
	 * 
	 * 
	 */
	public function getPubTemplateTitleList(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id'),
			'ids' => $request->get('ids'),
			'page' => $request->get('page'),
			'limit' => $request->get('limit')
		];

		$rpcData = self::$jsonRpcClient->getPubTemplateTitleList($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 查询商家账户里的可提现余额
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param int $env           0-正式环境 1-沙箱环境
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   amount[data]      可提现余额，单位元
	 * @return string   currency_code[data]      币种
	 * 
	 * 
	 */
	public function getXpayQueryBizBalance(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getXpayQueryBizBalance($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 绑定体验者
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $wechat_id 微信号
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   userstr[data]   人员对应的唯一字符串
	 * 
	 * 
	 */
	public function postBindTester(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'wechat_id' => $request->post('wechat_id')
		];

		$rpcData = self::$jsonRpcClient->postBindTester($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 解除绑定体验者
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * @param string $wechat_id 微信号。 userstr 和 wechatid 填写其中一个即可
	 * @param string $userstr 人员对应的唯一字符串， 可通过getTester接口获取人员对应的字符串
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 * 
	 */
	public function postUnBindTester(Request $request)
	{
		$params = [
			'app_id' => $request->post('app_id'),
			'wechat_id' => $request->post('wechat_id'),
			'userstr' => $request->post('userstr')
		];

		$rpcData = self::$jsonRpcClient->postUnBindTester($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 获取体验者列表
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   [data]   人员对应的唯一字符串
	 * 
	 * 
	 */
	public function getTester(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id')
		];

		$rpcData = self::$jsonRpcClient->getTester($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 获取提现列表
	 * 
	 * @author dotdotdot
	 * 
	 * @param string $app_id    app_id
	 * @param string $app_name 小程序名称
	 * @param int $company_entity_id 公司主体id
	 * @param int $status status 提现单的微信侧单号1-创建成功，提现中 2-提现成功 3-提现失败
	 * @param string $start_date 开始日期
	 * @param string $end_date 结束日期
	 * @param int $page
	 * @param int $limit
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   app_id[][data][data]   app_id 小程序appid
	 * @return string   app_name[][data][data]   app_name 小程序名称
	 * @return string   withdraw_no[][data][data]   withdraw_no 提现单号
	 * @return int   status[][data][data]   status 提现单的微信侧单号1-创建成功，提现中 2-提现成功 3-提现失败
	 * @return string   withdraw_amount[][data][data]   withdraw_amount 提现金额
	 * @return string   fail_reason[][data][data]   fail_reason  提现失败的原因
	 * @return string   create_time[][data][data]   create_time 创建时间
	 * @return string   update_time[][data][data]   update_time 更新时间
	 * @return int   count[data][data]   数量
	 * @return int   total_count[data][data]   总数
	 * 
	 * 
	 * 
	 */
	public function getXpayWithdrawNo(Request $request)
	{
		$params = [
			'app_id' => $request->get('app_id'),
			'app_name' => $request->get('app_name'),
			'company_entity_id' => $request->get('company_entity_id'),
			'status' => $request->get('status'),
			'start_date' => $request->get('start_date'),
			'end_date' => $request->get('end_date'),
			'page' => $request->get('page'),
			'limit' => $request->get('limit')
		];

		$rpcData = self::$jsonRpcClient->getXpayWithdrawNo($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 批量处理小程序
	 * 
	 * @author dotdotdot
	 * 
	 * @param array $app_ids    app_ids
	 * @param string $task_method 执行方法
	 * @param array $task_data 请求参数
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function postBatchTask(Request $request)
	{
		$params = [
			'app_ids' => $request->post('app_ids'),
			'task_method' => $request->post('task_method'),
			'task_data' => $request->post('task_data')
		];

		$rpcData = self::$jsonRpcClient->postBatchTask($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}

	/**
	 * 获取全部授权小程序
	 * 
	 * @author dotdotdot
	 * 
	 * @param int $company_entity_id    公司id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * @return string   app_id[][data][data]   app_id 小程序appid
	 * @return string   app_name[][data][data]   app_name 小程序名称
	 * @return int   company_entity_id[][data][data]   company_entity_id 公司id
	 * @return int   count[data][data]   数量
	 * @return int   total_count[data][data]   总数
	 * 
	 */
	public function getAuthorizersSelect(Request $request)
	{
		$params = [
			'company_entity_id' => $request->get('company_entity_id')
		];

		$rpcData = self::$jsonRpcClient->getAuthorizersSelect($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success($rpcData['data']);
	}

	/**
	 * 更新媒资视频地址
	 * 
	 * @author dotdotdot
	 * 
	 * @param int $video_id    短剧id
	 * 
	 * @return int      code     状态码
	 * @return string   msg      状态信息
	 * 
	 */
	public function postMiniDramaUrl(Request $request)
	{
		$params = [
			'video_id' => $request->post('video_id')
		];

		$rpcData = self::$jsonRpcClient->postMiniDramaUrl($params);

		if ($rpcData['code'] !== Error::SUCCESS_CODE) {
			return Common::fail($rpcData['msg'], $rpcData['code']);
		}

		return Common::success([], $rpcData['msg']);
	}
}
